package com.ucode.springboot.starter.web.validator.annotation;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * 自定义注解CheckFlag 的校验实现类
 * @author: liliang
 * @date: 2019年12月19日 上午11:45:49
 */
public class CheckMemberValidator implements ConstraintValidator<CheckMember, Object> {
    
    private String[] values;
    
    @Override
    public void initialize(CheckMember checkCase) {
        //初始化属性值
        this.values = checkCase.value();
    }

    @Override
    public boolean isValid(Object obj, ConstraintValidatorContext constraintValidatorContext) {
        if (obj == null) {
            return true;
        }
        
        boolean isValid = false;
        
        for (int i = 0; i < values.length; i++) {
            if (values[i].equals(obj.toString())) {
                isValid = true;
                break;
            }
        }
        return isValid;
    }
}